<template>
  <div>
    <!-- 旧票退回登记 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhOldInvoiceReturnRegist"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onCellChange="onCellChange"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" :disabled="!isCan" @click="returnHandle">退回登记</el-button>
        <multiuse-button size="small" type="primary" :disabled="!isSubmit" :click="signHandle">提 交</multiuse-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <SonModel :type="handleType" :rowData="formData" @close="emitCloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, registration } from '@/api/dealerFinance/InvoiceManage/OldInvoiceReturnRegist';
import SonModel from './SonModel';

export default {
  name: 'OldInvoiceReturnRegist',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    this.conf = { edit: '退回登记' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      visible: false,
      handleType: '',
      drawerTitle: '',
      isCan: false,
      isSubmit: false,
      submitData: []
    };
  },
  components: {
    SonModel
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    onCellChange(val, data) {
      this.isSubmit = this.BaseTable.GET_REQUIRED_ERROR() != null ? false : true;

      this.saveData(data, 60901004);
    },
    // 保存data
    async saveData(data, type) {
      let arrJson = [];

      if (type == 60901005) {
        data.map(x => {
          let json = {
            backCourierCompany: x.backCourierCompany,
            backCourierDate: x.backCourierDate,
            backCourierNo: x.backCourierNo,
            backMailNo: x.backMailNo || '',
            backRecipientId: x.backRecipientId || '',
            backRemark: x.backRemark,
            invoiceId: x.invoiceId,
            postType: type,
            powerType: x.powerType,
            buzinessType: x.buzinessType
          };
          arrJson.push(json);
        });
      } else {
        let json = {
          backCourierCompany: data.backCourierCompany,
          backCourierDate: data.backCourierDate,
          backCourierNo: data.backCourierNo,
          backMailNo: data.backMailNo || '',
          backRecipientId: data.backRecipientId || '',
          backRemark: data.backRemark,
          invoiceId: data.invoiceId,
          postType: type,
          powerType: data.powerType,
          buzinessType: data.buzinessType
        };
        arrJson.push(json);
      }
      console.log(arrJson, '---保存参数---');
      const res = await registration(arrJson);
      if (res.resultCode == 200) {
        if (type == 60901005) {
          this.$notify.success({ title: '提示信息', message: `提交成功！` });
          this.fetchParams = { ...this.fetchParams };
        }
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    returnHandle() {
      this.handleType = 'edit';
      this.formData = this.submitData;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    async signHandle() {
      await this.$confirm('是否提交', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await this.saveData(this.submitData, 60901005);
        })
        .catch(() => {});
    },
    selectChangeHandle(rows) {
      console.log(this.BaseTable.GET_REQUIRED_ERROR(), '---奥利给---');
      this.submitData = [];
      if (rows.length > 0) {
        if (rows.length == 1) {
          this.isCan = true;
          this.isSubmit = this.BaseTable.GET_REQUIRED_ERROR() != null ? false : true;
        }
        if (rows.length > 1) {
          let powerType = rows[0].powerType;
          let buzinessType = rows[0].buzinessType;

          this.isCan = rows.every(x => x.powerType == powerType && x.buzinessType == buzinessType);
          this.isSubmit = this.BaseTable.GET_REQUIRED_ERROR() != null ? false : true;
        }
        this.submitData = rows;
        return;
      }
      this.isCan = false;
      this.isSubmit = false;
    },
    emitCloseHandler(val, type) {
      this.visible = val;
      if (type == 'refresh') {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '原快递单号',
          fieldName: 'courierNo',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '原快递日期',
          fieldName: 'courierStrartDate|courierEndDate',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'INPUT',
          label: '发票号',
          fieldName: 'goldenInvoiceNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '快递公司',
          fieldName: 'courierCompany',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '业务类型',
          fieldName: 'buzinessType',
          placeholder: '请选择',
          itemList: this.createDictList('6089')
        },
        {
          type: 'SELECT',
          label: '邮寄状态',
          fieldName: 'postType',
          placeholder: '请选择',
          itemList: this.createDictList('6090').filter(x=>x.value == '60901004' || x.value == '60901007')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: props => {
            return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '业务类型',
          dataIndex: 'buzinessType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6089'),
          render: props => {
            return <span>{this.createDictText(props.row.buzinessType, '6089')}</span>;
          }
        },
        {
          title: '开票日期',
          dataIndex: 'invoiceDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '发票号码',
          dataIndex: 'goldenInvoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票代码',
          dataIndex: 'goldenInvoiceCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '邮寄状态',
          dataIndex: 'postType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6090'),
          render: props => {
            return <span>{this.createDictText(props.row.postType, '6090')}</span>;
          }
        },
        {
          title: '店端签收状态',
          dataIndex: 'receiptStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6091'),
          render: props => {
            return <span>{this.createDictText(props.row.receiptStatus, '6091')}</span>;
          }
        },
        {
          title: '异常原因',
          dataIndex: 'abnormalReason',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原快递公司',
          dataIndex: 'courierCompany',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退回驳回原因',
          dataIndex: 'rejectReason',
          sorter: true,
          filter: true,
          filterType: 'input',
         render: props => {
            return <span>{this.createDictText(props.row.rejectReason, '6090')}</span>;
          }
        },
        {
          title: '原快递单号',
          dataIndex: 'courierNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原快递日期',
          dataIndex: 'courierDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '退回快递公司',
          dataIndex: 'backCourierCompany',
          sorter: true,
          filter: true,
          filterType: 'input',
          editType: 'text',
          defaultEditable: true,
          editable: true,
          editRequired: true
        },
        {
          title: '退回快递单号',
          dataIndex: 'backCourierNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          editType: 'text',
          defaultEditable: true,
          editable: true,
          editRequired: true
        },
        {
          title: '退回快递日期',
          dataIndex: 'backCourierDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd',
          editType: 'date-picker',
          defaultEditable: true,
          editable: true,
          editRequired: true
        },
        {
          title: '备注',
          dataIndex: 'backRemark',
          sorter: true,
          filter: true,
          filterType: 'input',
          editType: 'text',
          defaultEditable: true,
          editable: true
        }
      ];
    }
  }
};
</script>
